<template>
  <div :class="{'hide-sider': hideSider}">
    <!-- 只控制侧栏收起/展开 -->
    <div class="custom-collapse-btn" @click="toggleSider">
      <a-icon :type="hideSider ? 'menu-unfold' : 'menu-fold'" />
    </div>
    <pro-layout
      :menus="menus"
      :i18nRender="i18nRender"
      :collapsed="collapsed"
      :handleMediaQuery="handleMediaQuery"
      :handleCollapse="handleCollapse"
      v-bind="settings"
    >
      <a-layout-content>
        <MultiTab v-show="$store.state.app.showTab"></MultiTab>
      </a-layout-content>

      <template v-slot:menuHeaderRender>
        <div class="tac">
          <img v-if="collapsed" class="logo" src="" />
          <!--<img v-if="collapsed" src="/logo.png" />-->
          <div v-else class="white fs20 pr24">{{ title }}</div>
        </div>
      </template>

      <template v-slot:headerContentRender>
        <div class="mt20 diy-mbx" v-show="$store.state.app.showNav">
          <a-breadcrumb>
            <a-breadcrumb-item v-for="(t,i) in routePathArr" :key="`route${i}`">
              <span v-if="i===0" >
                <a-tooltip title="首页">
                  <a @click="toPage('/')">
                    <i class="fa fa-home white" style="font-size:18px;"/>
                  </a>
                </a-tooltip>
              </span>
              <span v-else>{{ t.meta.title }}</span>
            </a-breadcrumb-item>
          </a-breadcrumb>
        </div>
      </template>

      <setting-drawer v-if="false" :settings="settings" @change="handleSettingChange">
        <div style="margin: 12px 0;">
          This is SettingDrawer custom footer content.
        </div>
      </setting-drawer>

      <template v-slot:rightContentRender>
        <right-content @refresh="refresh" :top-menu="settings.layout === 'topmenu'" :theme="settings.theme" />
      </template>

      <template v-slot:footerRender>
        <global-footer />
      </template>
      <div class="scroll pt10 pr10 pl10" :style="{margin:'-10px',height:`${winHeight-100}px`}">
        <router-view v-if="isRouterActive"/>
      </div>
    </pro-layout>
  </div>
</template>

<script>
import { SettingDrawer, updateTheme } from '@ant-design-vue/pro-layout'
import { i18nRender } from '@/locales'
import { mapState } from 'vuex'
import storage from 'store'
import { CONTENT_WIDTH_TYPE } from '@/store/mutation-types'
import MultiTab from '@/components/MultiTab'
import RightContent from '@/components/GlobalHeader/RightContent'
import GlobalFooter from '@/components/GlobalFooter'

export default {
  name: 'BasicLayout',
  components: {
    SettingDrawer,
    MultiTab,
    RightContent,
    GlobalFooter
  },
  data () {
    return {
      i18nRender,
      isRouterActive: true,
      // base
      menus: [],
      // 侧栏收起状态
      collapsed: false,
      title: '线上支付结算系统',
      hideSider: false,
      settings: {
        // 布局类型 'sidemenu'|'topmenu'
        layout: 'sidemenu',
        // 主题 'dark' | 'light'
        theme: 'light',
        primaryColor: storage.get('app.color', '#1890ff'),
        fixedHeader: true,
        fixSiderbar: true,
        colorWeak: false,
        hideHintAlert: false,
        hideCopyButton: false
      }
    }
  },
  computed: {
    ...mapState({
      // 动态主路由
      mainMenu: state => state.permission.addRouters,
      color: state => state.app.color
    }),
    routePathArr () {
      return this.$route.matched
    }
  },
  watch: {
    color (v) {
      this.setting.primaryColor = v
    }
  },
  created () {
    const routes = this.mainMenu.find(item => item.path === '/')
    this.menus = (routes && routes.children) || []
  },
  mounted () {
    const userAgent = navigator.userAgent
    if (userAgent.indexOf('Edge') > -1) {
      this.$nextTick(() => {
        this.collapsed = !this.collapsed
        setTimeout(() => {
          this.collapsed = !this.collapsed
        }, 16)
      })
    }
    if (process.env.NODE_ENV !== 'production') {
      updateTheme(this.settings.primaryColor)
    }
  },
  methods: {
    refresh () {
      this.isRouterActive = false
      this.$nextTick(() => {
        this.isRouterActive = true
      })
    },
    handleMediaQuery (val) {
      if (val['screen-xs']) {
        this.collapsed = false
        this.settings.contentWidth = CONTENT_WIDTH_TYPE.Fluid
      }
    },
    handleCollapse (val) {
      this.collapsed = val
    },
    handleSettingChange ({ type, value }) {
      type && (this.settings[type] = value)
      switch (type) {
        case 'contentWidth':
          this.settings[type] = value
          break
        case 'layout':
          if (value === 'sidemenu') {
            this.settings.contentWidth = CONTENT_WIDTH_TYPE.Fluid
          } else {
            this.settings.fixSiderbar = false
            this.settings.contentWidth = CONTENT_WIDTH_TYPE.Fixed
          }
          break
      }
    },
    toggleSider () {
      this.hideSider = !this.hideSider
    }
  }
}
</script>

<style lang="less">
@import "./BasicLayout.less";
.logo{
  width:40px;height:40px;position:relative;left:-12px;
}
.custom-collapse-btn {
  position: fixed;
  left: 200px; // 侧栏宽度
  bottom: 24px;
  z-index: 2000;
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 8px #f0f1f2;
  cursor: pointer;
  pointer-events: auto;
  transition: left 0.3s;
}
.hide-sider .ant-layout-sider {
  display: none !important;
}
.hide-sider .ant-layout-content,
.hide-sider .ant-pro-basicLayout-content,
.hide-sider .ant-layout,
.hide-sider .ant-pro-layout,
.hide-sider .ant-pro-basicLayout {
  margin-left: 0 !important;
  padding-left: 0 !important;
  width: 100% !important;
  min-width: 0 !important;
  max-width: 100% !important;
  flex: 1 1 0 !important;
}
.hide-sider .ant-pro-sider,
.hide-sider .ant-pro-sider-menu,
.hide-sider .ant-pro-sider-menu-logo,
.hide-sider .ant-pro-global-header-trigger {
  display: none !important;
}
.hide-sider .ant-pro-global-header-logo {
  display: none !important;
}
.hide-sider .ant-layout {
  width: 100% !important;
  max-width: 100% !important;
}
.hide-sider .ant-layout.ant-layout-has-sider {
  width: 100% !important;
  max-width: 100% !important;
}
.hide-sider [style*="width: calc(100% - 256px)"] {
  width: 100% !important;
  max-width: 100% !important;
}
</style>
